package com.fitbet.entity.bet;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import com.demo2do.core.format.PersistentFormat;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * 大赌局
 * 
 * @author sjh
 *
 */
@Entity
@Table(name = "bet")
@PersistentFormat
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Bet {

    @Id
    @GeneratedValue
    private Long id;

    /**
     * 图片
     */
    private String avatar;

    /**
     * 赌局名称
     */
    private String name;

    /**
     * 开始时间呢
     */
    @Type(type = "date")
    private Date startDate;

    /**
     * 结束时间
     */
    @Type(type = "date")
    private Date endDate;

    /**
     * 状态
     */
    @Enumerated(EnumType.ORDINAL)
    private BetStatus status;

    /**
     * 奖金池
     */
    private int bonus;
    
    /**
     * 参与人数
     */
    private int thumbAmount;

    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Bet() {
        
    }
    
   
    /**
     * 
     * @return
     */
    @Transient
    public BigDecimal getBetBonus() {
    	
    	BigDecimal betBonus = new BigDecimal(this.bonus).divide(new BigDecimal(100), 2 , BigDecimal.ROUND_HALF_UP);
    	
    	return betBonus;
    }
    
    /**
     * 
     * @return
     */
    public boolean isEnd() {
        Date now = new Date();
        return this.endDate.equals(now) || this.endDate.before(now);
    }
    
    /**
     * 
     * @return
     */
    public Bet openBet() {
    	this.status = BetStatus.PROCESSING;
    	return this;
    }
    
    
    /**
     * 
     * @return
     */
    public Bet closeBet() {
    	this.status = BetStatus.FINISHED;
    	return this;
    }
    
    /**
     * 
     * @param mediaId
     * @return
     */
    public Bet editCover(String media) {
    	this.avatar = media;
    	return this;
    }
    
    /**
     * 
     * @return
     */
    public List<String> getThumbAmountSplit() {
        String thumbAmountString = new BigDecimal(this.thumbAmount).toString();
        char[] chars = thumbAmountString.toCharArray();
        List<String> result = new LinkedList<>();
        for(int i = 0;i<chars.length;i++){
            char item = chars[i];
            result.add(String.valueOf(item));
        }
        return result;
    }
    
    /**
     * 
     * @return
     */
    public List<String> getBonusSplit() {
        String bonusString = String.valueOf((this.bonus / 100));
        char[] chars = bonusString.toCharArray();
        List<String> result = new LinkedList<>();
        for(int i = 0;i<chars.length;i++){
            char item = chars[i];
            result.add(String.valueOf(item));
        }
        return result;
    }
    
    public Bet addThumb(int price) {
        this.bonus = this.bonus + price;
        this.thumbAmount = this.thumbAmount + 1;
        return this;
    }
    
    public String getEndDateString() {
        String result = DateFormatUtils.format(this.endDate, "yyyy/MM/dd") + " 23:59:59";
        return result;
    }
    
    public List<String> getRestDays() {
        
        List<String> result = new LinkedList<String>();
        
        Date endDate = DateUtils.parseDate(DateFormatUtils.format(this.endDate, "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
        long times = endDate.getTime() - new Date().getTime();
        
        if(times > 0) {
            int days = 0;
            int dayUnit = 1000 * 60 * 60 * 24; 
            
            days = (int) (times / dayUnit);
            result.add(days >= 10 ? String.valueOf(days) : ("0" + String.valueOf(days)));
            
            
            int hourRest = (int) (times - dayUnit * days);
            int hourUnit = 1000 * 60 * 60;
            int hours  = hourRest / hourUnit;
            
            result.add(hours >= 10 ? String.valueOf(hours) : ("0" + String.valueOf(hours)));
            
            int minutesRest = hourRest - hours*hourUnit;
            int minutesUnit = 1000 * 60;
            int minutes = minutesRest / minutesUnit;
            
            result.add(minutes >= 10 ? String.valueOf(minutes) : ("0" + String.valueOf(minutes)));
            
        } else {
            result.add("00");
            result.add("00");
            result.add("00");
        }
        
        return result;
        
        
        
    }
    
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }
    
    /**
     * @return the avatar
     */
    public String getAvatar() {
        return avatar;
    }
    
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    
    /**
     * @return the startDate
     */
    public Date getStartDate() {
        return startDate;
    }



    /**
     * @return the endDate
     */
    public Date getEndDate() {
        return endDate;
    }



    /**
     * @return the status
     */
    public BetStatus getStatus() {
        return status;
    }



    /**
     * @return the bonus
     */
    public int getBonus() {
        return bonus;
    }
    
    /**
     * @return the thumbAmount
     */
    public int getThumbAmount() {
        return thumbAmount;
    }
    
    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }
    
    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    /**
     * @param avatar the avatar to set
     */
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    
    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    
    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    
    /**
     * @param status the status to set
     */
    public void setStatus(BetStatus status) {
        this.status = status;
    }
    
    /**
     * @param bonus the bonus to set
     */
    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
    
    /**
     * @param thumbAmount the thumbAmount to set
     */
    public void setThumbAmount(int thumbAmount) {
        this.thumbAmount = thumbAmount;
    }
    
    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
